package controller;

import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;




    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/dologin")
    public String dologin(//User user,
            @RequestParam(value = "userName",required = false)String userName,
            @RequestParam(value = "userPass",required = false)String userPass,
            HttpSession session,
            Model model) {
        if (!userService.isUserExist(userName, userPass)) {
            model.addAttribute("mag", "用户名不存在!");
            return "login";
        } else {
            if (userPass.equals(userService.getUser(userName, userPass).getUserPass())) {
                model.addAttribute("mag", "登录成功!");
            } else {
                model.addAttribute("mag", "登录失败!");
                return "login";
            }
        }
        return "redirect:/hotel/hotelList";
    }



    @RequestMapping("/zhuce")
    public String zhuce(){
        return "zhuce";
    }

    @RequestMapping("/dozhuce")
    public String dozhuce(User user,
                          @RequestParam("userName")String userName,
                          @RequestParam("userPass")String userPass){
        if(userName!=null && userPass!=null){
            userService.userAdd(user);
        }
        return "redirect:login";
    }

    @RequestMapping("/xiugai")
    public String xiugai(){
        return "xiugai";
    }

    @RequestMapping("/doxiugai")
    public String doxiugai(@RequestParam("userName")String userName,
                           @RequestParam("userPass")String userPass,
                           @RequestParam("newuserPass")String newuserPass, Model model,User user){
        if(!userService.isUserExist(userName,userPass)){
            model.addAttribute("mag","用户名或密码不存在!");
        }else{
            if(userPass.equals(userService.getUser(userName, userPass).getUserPass())){
                user.setUserPass(newuserPass);
                user.setUserName(userName);
                userService.UpUserPass(user);
                model.addAttribute("mag","修改成功!");
            }else{
                model.addAttribute("mag","密码错误!");
            }

        }
        return "login";
    }



}
